<?php
########################################################################################
# Empresa :     Tecnopac Informatica;                                                  #
# Autor:        Deiverson Pacheco;                                                     #
# Data Criação: 10/12/2010;                                                            #
########################################################################################
session_start();
header("Cache-control: private");
include_once("../../includes/session.php");


//Linha para formatação de caracteres com acentuação
header("Content-Type: text/html; charset=ISO-8859-1",true);


//======================================================================================
//==> Include obrigatórias no script
//======================================================================================
include_once('../../includes/classes/class.TemplatePower.php');
include_once('../../includes/classes/class.Conexao.php');
include_once('../../includes/classes/class.Geral.php');

//======================================================================================
//==> Inicializar Classes
//======================================================================================
$not  = new Persist;
$not2 = new Persist;
$data = new getData;
$cor  = new ColorTr;
$util = new Util;
$vld  = new ValidarDados;
$sql  = new SqlInjection;

//======================================================================================
//==> Instanciar TemplatePower para juntar o PHP + HTML
//======================================================================================
$tpl  = new TemplatePower('../../html/sistema/popup_grupos_permissoes.html');
$tpl->prepare();


//======================================================================================
//==> Recebe variaveis de pesquisa
//======================================================================================
$id  = @$_GET['id'];
$evento  = @$_GET['evento'];

$tpl->assign('id',$id);

switch ($evento){
	case 'lista':
		$not->sql = "SELECT * FROM menu";
		if($not->ler() > 0){
			while($not->ini()){
				$tpl->newBlock("LOOP");
				$tpl->assign('id',$not->id);
				$tpl->assign('title',$not->ds_menu);
			}
		}
	break;
	
	case 'carrega_menu':
		$id_menu  = @$_GET['id_menu'];
		
		$not->sql = "SELECT * FROM submenu
							WHERE id_menu = $id_menu";
		
		if($not->ler() > 0){
			while($not->ini()){
				$not2->sql = "SELECT id FROM
								 grupos_permissoes_menu
								 	WHERE id_grupo = $id AND id_submenu = ". $not->id;
									
				print "<input type='checkbox' 
					name='chk_".$not->id."' id='chk_".$not->id."' ".($not2->ler() > 0 ? ' checked ' : '')."
					onclick='altera_menus(this.checked,".$not->id.")' >
					".$not->ds_submenu." <br>";
			}
		}
		exit;
	break;
	
	case 'altera_menu':
		$id_submenu  = @$_GET['id_menu'];
		$act  = @$_GET['act'];
		
		$not->sql = "SELECT id_menu FROM submenu
							WHERE id = $id_submenu";
		$not->ler();
		$not->ini();
		
		if($act == "true"){

			$not->sql = " INSERT INTO grupos_permissoes_menu
							(id_grupo,id_menu,id_submenu)
							VALUES
							($id,$not->id_menu,$id_submenu)";
			$not->ins();
		}else{
			
			$not->sql = " DELETE FROM grupos_permissoes_menu
							WHERE id_grupo = $id AND id_menu = $not->id_menu AND id_submenu = $id_submenu";
			$not->del();
		}
		
		exit;
	break;
}
//======================================================================================
//==> Enviar página unificada PHP + HTML
//======================================================================================
$tpl->showUnAssigned( false );  // para debug. na hora de publicar, setar para false
$tpl->printToScreen();
?>